/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.easyedu.dictionary.explorer.nodes;

import eu.easyedu.dictionary.api.importer.wizard.ImporterWizardAction;
import eu.easyedu.dictionary.api.resources.Bundle;
import eu.easyedu.dictionary.api.service.events.DictionaryEventListener;
import eu.easyedu.dictionary.explorer.DictionaryList;
import eu.easyedu.dictionary.explorer.actions.AddDictionaryAction;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.ImageUtilities;
import org.openide.windows.WindowManager;

/**
 *
 * @author hlavki
 */
public class DictionaryRootNode extends AbstractNode implements DictionaryEventListener {

    private static final String ROOT_DESCRIPTION_PROP = "DictionaryExplorer.rootNode.description";
    private static final String ICON_PATH_PROP = Bundle.getResourcesPath() + "/dictionaries16x16.png";
    private static DictionaryRootNode node;

    private DictionaryRootNode() {
        super(Children.create(new RootDictionaryFactory(), true));
        WindowManager.getDefault().invokeWhenUIReady(new Runnable() {

            public void run() {
                refresh();
            }
        });
        DictionaryList.getDefault().addDictionaryEventListener(this);
    }

    public static synchronized DictionaryRootNode getDictionaryRootNode() {
        if (node == null) {
            node = new DictionaryRootNode();
        }
        return node;
    }

    @Override
    public String getDisplayName() {
        return Bundle.getMessage(ROOT_DESCRIPTION_PROP);
    }

    @Override
    public String getShortDescription() {
        return "Short desc";
    }

    @Override
    public Image getIcon(int type) {
        return ImageUtilities.loadImage(ICON_PATH_PROP);
    }

    @Override
    public Image getOpenedIcon(int type) {
        return ImageUtilities.loadImage(ICON_PATH_PROP);
    }

    @Override
    public Action[] getActions(boolean context) {
        Action[] actions = super.getActions(context);
        List<Action> actionList = Arrays.asList(actions);
        List<Action> newActions = new ArrayList<Action>();
        newActions.add(new AddDictionaryAction());
        newActions.add(new ImporterWizardAction());
        newActions.add(new RefreshAction());
        newActions.add(null);
        newActions.addAll(actionList);
        return newActions.toArray(new Action[0]);
    }

    public synchronized void refresh() {
        setChildren(Children.create(new DictionaryFactory(), true));
    }

    public class RefreshAction extends AbstractAction {

        private static final long serialVersionUID = 1L;

        public RefreshAction() {
            putValue(NAME, "Refresh");
        }

        public void actionPerformed(ActionEvent e) {
            refresh();
        }
    }

    public void dictionaryChanged() {
        refresh();
    }
}
